<template>
  <el-dialog v-model="visible" title="打印记录" width="800">
    <el-table
      stripe
      border
      ref="tableRef"
      v-loading="loading"
      :data="tableData"
      key="id"
    >
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column prop="createTime" label="打印时间" />
      <el-table-column prop="type" label="打印内容">
        <template v-slot:default="{ row }">
          {{ row.type === 0 ? "二维码+编码码" : "编码码" }}
        </template>
      </el-table-column>
      <el-table-column prop="actions" label="操作" width="180">
        <template v-slot:default="{ row }">
          <el-button type="primary" size="small" text @click="onExport(row)">
            导出
          </el-button>
          <el-button type="success" size="small" text @click="onPrint(row)">
            打印
          </el-button>
          <el-button
            type="danger"
            size="small"
            text
            @click="onPrintLogDetail(row)"
          >
            明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <DialogPrint
      ref="dialogPrintRef"
      url="/admin-api/system/label-print/printByBatchno"
      :batchno="batchno"
    ></DialogPrint>

    <DialogDetail ref="dialogDetailRef" :batchno="batchno"></DialogDetail>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import api from "@/api/label.js";
import { downloadFn } from "@/api/index.js";
import { useRouter } from "vue-router";
import DialogPrint from "./dialogPrint.vue";
import DialogDetail from "./dialogDetail.vue";

const props = defineProps({
  typeName: { type: String, default: "新增" },
});
const visible = ref(false);
const tableData = ref([]);
const dialogPrintRef = ref();
const tableRef = ref();
const total = ref(0);
const batchno = ref();
const dialogDetailRef = ref();
const router = useRouter();

const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});
const loading = ref(false);
const emits = defineEmits(["success"]);

const initData = async () => {
  loading.value = true;
  try {
    const res = await api.printLog(queryData.value);
    tableData.value = res.data.list;
    total.value = res.data.total;
  } finally {
    loading.value = false;
  }
};

const onPrint = (row) => {
  // batchno.value = row.batchno;
  // dialogPrintRef.value.openDialog(row.batchno);
  router.push({
    path: "/printLabel",
    query: {
      batchno: row.batchno,
    },
  });
};

const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData();
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData();
};

const onExport = async (row) => {
  const res = await downloadFn(
    "/admin-api/system/label-print/exportByBatchno",
    {
      batchno: row.batchno,
    }
  );
  const blob = new Blob([res], {
    type: "text/plain;charset=utf-8",
  });
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.href = url;
  link.download = `${row.batchno}printLog` + new Date().getTime();
  link.click();
  window.URL.revokeObjectURL(url);
  ElMessage.success(`导出成功！`);
};

const onPrintLogDetail = (row) => {
  batchno.value = row.batchno;
  dialogDetailRef.value.openDialog(row);
};

const openDialog = () => {
  visible.value = true;
  tableData.value = [];
  handleCurrentChange(1);
};

defineExpose({
  openDialog,
});
</script>
